<template>
  <div class="register">
    <cu-header :height="80" />
    <div class="register-container container">
      <div class="register-form">
        <p class="title">{{ $t("register.1") }}</p>
        <div class="form">
          <el-form ref="form" :model="form" :rules="rules" label-width="200px">
            <el-form-item :label="$t('register.2')" prop="avatar">
              <el-upload class="avatar-uploader" :action="uploadUrl" :show-file-list="false" :on-success="handleAvatarSuccess">
                <img v-if="form.avatar" :src="domain + form.avatar" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item :label="$t('register.3')" prop="email">
              <el-input type="text" v-model="form.email" :placeholder="$t('register.12')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.4')" prop="mobile">
              <el-input type="text" v-model="form.mobile" :placeholder="$t('register.13')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.5')" prop="password">
              <el-input type="password" v-model="form.password" :placeholder="$t('register.14')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.6')" prop="rePassword">
              <el-input type="password" v-model="form.rePassword" :placeholder="$t('register.15')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.7')" prop="no">
              <el-input type="text" v-model="form.no" :placeholder="$t('register.16')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('register.8')" prop="area">
              <el-cascader ref="area" v-model="form.area" :options="multiOptions" :props="{ value: 'code', label: 'name' }" :placeholder="$t('register.17')" @change="handleMultiChange" style="width: 100%"></el-cascader>
            </el-form-item>
            <!-- <el-form-item :label="$t('register.9')" prop="address">
              <el-input type="text" v-model="form.address" :placeholder="$t('register.18')"></el-input>
            </el-form-item> -->
            <el-form-item class="text-center">
              <el-button type="default" @click="handleBack">{{ $t("register.10") }}</el-button>
              <el-button type="primary" @click="handleSubmit">{{ $t("register.11") }}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <cu-footer />
  </div>
</template>
<script>
import CuHeader from "@/components/header";
import CuFooter from "@/components/footer";
import Api from "@/api";
import { Domain } from "@/constants";
import { isEmail } from "@/utils";
export default {
  name: "Register",
  components: { CuHeader, CuFooter },
  data() {
    return {
      domain: Domain,
      uploadUrl: Api.uploadUrl,
      form: {
        avatar: "",
        password: "",
        rePassword: "",
        no: "",
        mobile: "",
        email: "",
        area: [],
        address: "",
      },
      rules: {
        avatar: { required: true, message: window.$lang.t("register.19"), trigger: "change" },
        password: { required: true, message: window.$lang.t("register.20"), trigger: "blur" },
        rePassword: [
          { required: true, message: window.$lang.t("register.21"), trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (this.form.password !== value) {
                callback(new Error(window.$lang.t("register.22")));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        no: { required: true, message: window.$lang.t("register.23"), trigger: "blur" },
        mobile: [
          { required: true, message: window.$lang.t("register.24"), trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (!/\d{9}/.test(value)) {
                callback(new Error(window.$lang.t("register.29")));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: window.$lang.t("register.25"), trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (!isEmail(value)) {
                callback(new Error(window.$lang.t("register.26")));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        area: { required: true, message: window.$lang.t("register.27"), trigger: "change" },
        address: { required: true, message: window.$lang.t("register.28"), trigger: "blur" },
      },
      multiOptions: [],
    };
  },
  created() {
    this.loadTree();
  },
  methods: {
    handleAvatarSuccess(file) {
      if (file) {
        this.form.avatar = file.path;
      }
    },
    loadTree() {
      Api.areaTree().then((res) => {
        this.multiOptions = res;
      });
    },
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.form.areaCode = this.form.area[this.form.area.length - 1];
          Api.register(this.form).then((res) => {
            console.log(res);
            if (res) {
              window.localStorage.setItem("TOKEN", res.token);
              this.$router.push("/");
            }
          });
        } else {
          return false;
        }
      });
    },
    handleBack() {
      this.$router.go(-1);
    },
    handleMultiChange(value) {
      this.form.area = value;
      this.form.areaCode = "/" + this.form.area.join("/") + "/";
      this.form.areaName = this.$refs.area.getCheckedNodes()[0].pathLabels.join("/");
    },
  },
};
</script>
<style lang="scss" scoped>
.register {
  min-height: 100vh;
  background: #f2f2f2;
  .register-container {
    padding: 50px 0;
    .register-form {
      padding: 20px;
      background: #fff;
      border-radius: 4px;
      .title {
        color: #333;
        font-weight: bold;
        font-size: 16px;
        padding-left: 15px;
        margin-top: 20px;
        margin-bottom: 20px;
      }
      .form {
        width: 60%;
      }
    }
  }
  .avatar-uploader {
    ::v-deep .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      &:hover {
        border-color: #fb8c24;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
      }
      .avatar {
        width: 80px;
        height: 80px;
        display: block;
      }
    }
  }
}
</style>
